$("#wrapper").attr("style","width: 100% !important;min-width: 500px;");
$("a#login_logo").css({"margin-top":0});
if((/android|webos|iphone|ipad|ipod|blackberry/i.test(navigator.userAgent.toLowerCase()))){
	$("#login_form").remove();
	$("div#result").attr("class","").addClass("alert alert_red").html('This system is only accessible through PC or Mac systems!');
	$("#wrapper").attr("style","width: 100% !important;min-width: 100%;");
	$(".isolate>.narrow").attr("style","width: 100% !important;");
	$(".isolate .center .main_container").attr("style","margin:0 auto !important;");
	$("a#login_logo").css({"margin": "0 auto","float":"right"});
}
$("#login_form").submit(function(e){
	e.preventDefault();
	var e = $("input#email").val();
	var p = $("input#password").val();
	var r = $("input#remember_me").is(':checked');
	var alert = '<img height="24" width="24" src="images/icons/small/white/alert.png">';
	var waiting = '<img height="24" width="24" src="images/icons/small/white/alarm_clock.png">';
	function ive(e){
	    var p = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
	    return p.test(e);
	}
	if(!ive(e)){
		$("div#result").attr("class","").addClass("alert alert_red").html('<img height="24" width="24" src="images/icons/small/white/alert.png">Please enter an email address in the format someone@example.com!');
	  	$("button#submit span").html("Login").parent().prop("disabled", false);
	  	$('label').css('bottom','auto');
	}else if(p.length < 4){
		$("div#result").attr("class","").addClass("alert alert_red").html('<img height="24" width="24" src="images/icons/small/white/alert.png">Password length must be at least 4 characters!');
	  	$("button#submit span").html("Login").parent().prop("disabled", false);
	  	$('label').css('bottom','auto');
	}else
	window.ajax = $.ajax({
	    url: './ajax?r=login',
	    type: 'POST',
	    sync: true,
	    data: {email: e, password: p, remember_me: r},
	    beforeSend: function ( xhr ) {
	    	$("div#result").attr("class","").addClass("alert alert_orange").html(waiting + "Validating Login details, please wait....");
	    	$("button#submit span").html("Please wait....").parent().prop("disabled", true);
	  	},
	  	fail: function(data){
	  		$("div#result").attr("class","").addClass("alert alert_red").html(alert + 'Internal Error [AJAX Failure], Please call Administrator!');
	  		$("button#submit span").html("Login").parent().prop("disabled", false);
	  	},
	  	error: function(data){
	  		$("div#result").attr("class","").addClass("alert alert_red").html(alert + 'Internal Error [AJAX Error], Please call Administrator!');
	  		$("button#submit span").html("Login").parent().prop("disabled", false);
	  	},
	    success: function(data){
	    	var redirect = "./";
	    	$("button#submit span").html("Login").parent().prop("disabled", false);
	    	if (data['data'] === undefined)
	    		data = {'error': 500,'data': 'Internal Error [500], Please call Administrator!'};
	    	if(data['error']){
	    		$("div#result").attr("class","").addClass("alert alert_red").html(alert + data['data']);
	    	}else if(data['error'] == 0){
	    		$("button#submit span").html("Redirecting..").parent().prop("disabled", true);
	    		$("div#result").attr("class","").addClass("alert alert_green").html(data['data']);
	    		if($("input#redir").val())
	    			redirect = $("input#redir").val(); 
	    		if($("input#child") && $("input#child").val()=="true"){
	    			if(window.opener.formNeedSubmit)
	    				window.opener.formNeedSubmit.submit();
	    			window.close();
	    		}else{
	    			if(!redirect || redirect.length <= 2)
	    				redirect = "./";
    				$("#wrapper").append("<a id='redir_link' href='"+redirect+"' class='pjax pjax_on'>redir</a>");
    				$("#redir_link").click();	
	    			//window.location.replace(redir);
	    		}
	    	}
	    }
	});
});